import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:school_assistant/common_colors.dart';
import 'package:school_assistant/user_feedback_imgs.dart';
import 'package:sp_util/sp_util.dart';

import 'common_client.dart';
import 'data/uer_info_entity.dart';
import 'info_page.dart';
import 'login_page.dart';
import 'my_article.dart';
import 'order_list.dart';

class Minepage extends StatefulWidget {
  const Minepage({Key? key}) : super(key: key);

  @override
  State<Minepage> createState() => _MinepageState();
}

class _MinepageState extends State<Minepage> {
  UerInfoEntity? uerInfo;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: CommonColor.colorF5,
      body: Stack(
        children: [
          Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: Container(
                alignment: Alignment.centerRight,
                color: Color(0xff2f2f2f),
                height: 200,
                child: Image.asset(
                  UserFeedBackImgs.icon_home_top_bg,
                  fit: BoxFit.fitHeight,
                ),
              )),
          Positioned(
            top: 80,
            left: 20,
            right: 0,
            child: Row(
              children: [
                CachedNetworkImage(
                    imageBuilder: (context, image) => CircleAvatar(
                          backgroundImage: image,
                        ),
                    errorWidget: (context, url, error) => CircleAvatar(
                          backgroundImage:
                              AssetImage(UserFeedBackImgs.user_male),
                        ),
                    width: 70,
                    height: 70,
                    imageUrl: UserFeedBackImgs.url + (uerInfo?.profile ?? '')),
                SizedBox(
                  width: 12,
                ),
                RichText(
                    text: TextSpan(children: [
                  TextSpan(
                      text: uerInfo?.username ?? "姓名",
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: 15,
                          height: 2,
                          fontWeight: FontWeight.w600)),
                  TextSpan(
                    text: "\n",
                  ),
                  TextSpan(
                      text: uerInfo?.email ?? "邮箱",
                      style: TextStyle(
                          color: Colors.white, height: 2, fontSize: 15))
                ]))
              ],
            ),
          ),
          Positioned(
            top: 180,
            left: 20,
            right: 20,
            child: Container(
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(5)),
              child: Column(
                children: [
                  SizedBox(
                    height: 10,
                  ),
                  GestureDetector(
                    onTap: () {
                      Get.to(PriInfoPage())?.then((value) => {
                        _getUser()

                      });
                    },
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        SizedBox(
                          width: 20,
                        ),
                        Image.asset(
                          UserFeedBackImgs.ic_info,
                          width: 30,
                        ),
                        SizedBox(
                          width: 6,
                        ),
                        Text(
                          '个人信息',
                          textAlign: TextAlign.center,
                        ),
                        Spacer(),
                        Icon(
                          Icons.arrow_forward_ios,
                          size: 13,
                        ),
                        SizedBox(
                          width: 20,
                        )
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Divider(
                    height: 1,
                    color: CommonColor.colorEE,
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  GestureDetector(
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        SizedBox(
                          width: 20,
                        ),
                        Image.asset(
                          UserFeedBackImgs.ic_order,
                          width: 30,
                          fit: BoxFit.fitWidth,
                        ),
                        SizedBox(
                          width: 6,
                        ),
                        Text(
                          '我的订单',
                          textAlign: TextAlign.center,
                        ),
                        Spacer(),
                        Icon(
                          Icons.arrow_forward_ios,
                          size: 13,
                        ),
                        SizedBox(
                          width: 20,
                        )
                      ],
                    ),
                    onTap: () {
                      Get.to(OrderList());
                    },
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Divider(
                    height: 1,
                    color: CommonColor.colorEE,
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  GestureDetector(
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        SizedBox(
                          width: 20,
                        ),
                        Image.asset(
                          UserFeedBackImgs.ic_post,
                          width: 30,
                        ),
                        SizedBox(
                          width: 6,
                        ),
                        Text(
                          '我的帖子',
                          textAlign: TextAlign.center,
                        ),
                        Spacer(),
                        Icon(
                          Icons.arrow_forward_ios,
                          size: 13,
                        ),
                        SizedBox(
                          width: 20,
                        )
                      ],
                    ),
                    onTap: () {
                      Get.to(MyActicle());
                    },
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Divider(
                    height: 1,
                    color: CommonColor.colorEE,
                  )
                ],
              ),
            ),
          ),
          Positioned(
            left: 0,
            right: 0,
            bottom: 60,
            child: GestureDetector(
              onTap: () {
                SpUtil.putString("uid", '');
                Get.offAll(LoginPage(),
                    transition: Transition.rightToLeftWithFade);
              },
              child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(left: 40, right: 40),
                height: 40,
                decoration: BoxDecoration(
                    color: Color(0xff83DF00),
                    borderRadius: BorderRadius.circular(4)),
                child: Text(
                  "退出登录",
                  style: TextStyle(
                      color: Color(0XFF333333),
                      fontSize: 16,
                      fontWeight: FontWeight.w500),
                ),
              ),
            ),
          ),
          Positioned(
            left: 0,
            right: 0,
            bottom: 20,
            child: Text(
              'v1.0.0',
              textAlign: TextAlign.center,
            ),
          )
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    _getUser();
  }

  _getUser() async {
    try {
      var res = await CommonClient().getUser(SpUtil.getString("uid") ?? '');
      print(jsonEncode(res.data ?? ""));
      List<dynamic> data = jsonDecode(res.data ?? "");
      if (data != null) {
        uerInfo = UerInfoEntity.fromJson(data[0]);
        setState(() {});
      }
    } catch (e) {
      print(e);
    }
  }
}
